<?php

namespace App\Http\Controllers\Api;

use App\Http\Services\UserManager;
use App\OrderCar;
use App\orders;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
    public function getServiceOrderList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);
        $page   =$request->input('page') ? $request->input('page') : 1;
        $limit  =$request->input('limit') ? $request->input('limit') : 5;
        $status =$request->input('status');
        $orderCars=OrderCar::where(1);
        $page_info=[
            'page'=>$page,
            'limit'=>$limit,
            'total'=>0
        ];
        $page=$page-1;
        if (in_array($status,[0,1,2,3,])){
            $orderCars=$orderCars->whereStatus($status);
        }
        $page_info['total']=$orderCars->count();

        $orderCars=$orderCars->orderByDesc('id')->offset($page * $limit)->limit($limit)->get();
        if (count($orderCars)> 0){
            /** @var OrderCar $orderCar */
            foreach ($orderCars as $orderCar){
                $orderCar->order_info   =OrderController::getOrderDetail($orderCar->order_id);
                $orderCar->pets_info    =PetsController::getOrderPets($orderCar->order_id);
                $orderCar->car_info     =DriverController::getCarInfoByCarId($orderCar->car_id);
                $orderCar->service_order_status    = DriverController::getDriverOrderStatusName($orderCar->status);
            }
        }
        return response()->json(['level'=>'success','message'=>'success','data'=>$orderCars,'page_info'=>$page_info]);
    }

    public function dispatchOrderToDriver(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);
        $order_id   =$request->input('order_id');
        $driver_id  =$request->input('driver_id');
        $remark     =$request->input('remark');

        if (!$order_id || !$driver_id)
            return response()->json(['level' => 'error','message' => '参数错误']);
        $thisOrder=orders::whereId($order_id)->first();
        //TODO 判断订单状态是否符合派单要求

        //TODO 分配司机
        if (OrderCar::whereOrderId($order_id)->count() >=1)
            return response()->json(['level' => 'error','message' => '已分配司机，请使用改派']);
        $orderCar=OrderCar::whereOrderId($order_id)->orderByDesc('id')->first();
        if (!$orderCar){
            $orderCar=new OrderCar();
        }
        $orderCar->order_id =$order_id;
        $orderCar->driver_id=$driver_id;
        $thisDriversCar=DriverController::getDriverCarByDriverId($driver_id);
        if ($thisDriversCar)
            $orderCar->car_id   =$thisDriversCar->id;
        $orderCar->remark   =$remark;
        $orderCar->save();
        return response()->json(['level'=>'success','message'=>'success','data'=>$orderCar]);

    }
}
